package com.wontube.smr.remote.admin;

import java.util.List;

import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.cache.Cacher;
import com.wontube.core.cache.EHCacheKey;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.LocationBO;
import com.wontube.smr.client.IDeviceHandler;
import com.wontube.smr.client.IVideoHandler;
import com.wontube.smr.client.model.po.Country;
import com.wontube.smr.client.model.po.WebsiteCategory;
import com.wontube.smr.client.model.po.WebsiteLink;
import com.wontube.smr.dao.bo.CountryDao;
import com.wontube.smr.dao.bo.WebsiteCategoryDao;
import com.wontube.smr.dao.bo.WebsiteLinkSelectDao;
import com.wontube.smr.web.client.admin.IAdminWebsiteLinkHandler;
import com.wontube.smr.web.client.m.IWebsiteLinkHandler;


@NoLoginSupport
public class AdminWebsiteLinkHandler extends BasicHandler implements IAdminWebsiteLinkHandler {
	@Override
	public List<WebsiteLink> listWebsiteLinks(String countryCode, String sysId,
			String category) {
		return WebsiteLinkSelectDao.listAllWebsiteLinks(countryCode, sysId, category);
	}
	
	@Override
	public boolean saveOrUpdateWebsiteLink(WebsiteLink website) {
		boolean success = WebsiteLinkSelectDao.saveOrUpdateWebsiteLink(website);
//		if(success){
//			clearDeviceCaches();
//		}
		return success;
	}

	@Override
	public boolean deleteWebsiteLink(String pk) {
		boolean success = WebsiteLinkSelectDao.deleteWebsiteLink(pk);
//		if(success){
//			clearDeviceCaches();
//		}
		return success;
	}

	@Override
	public WebsiteLink getWebsiteLinkByPK(String pk) {
		return WebsiteLinkSelectDao.getWebsiteLinkByPK(pk);
	}
	
	@Override
	public boolean clearDeviceCaches(){
		String clazzName = IDeviceHandler.class.getName();	
		Cacher.defaultCacher.deleteCache(clazzName, "listDeviceHomeLinks", new EHCacheKey(new Object[0]));
		Cacher.defaultCacher.deleteAllCache(clazzName, "listDeviceHomeLinks", 1);
		Cacher.defaultCacher.deleteAllCache(clazzName, "listDeviceHomeLinks", 2);
		
		Cacher.defaultCacher.deleteCache(clazzName, "listMoreLinks", new EHCacheKey(new Object[0]));
		Cacher.defaultCacher.deleteAllCache(clazzName, "listMoreLinks", 1);		
		Cacher.defaultCacher.deleteAllCache(clazzName, "listMoreLinks", 2);	
		
		Cacher.defaultCacher.deleteCache(clazzName, "listAllCategory", new EHCacheKey(new Object[0]));
		Cacher.defaultCacher.deleteAllCache(clazzName, "listAllCategory", 1);
		Cacher.defaultCacher.deleteCache(clazzName, "listCountries", new EHCacheKey(new Object[0]));
		
		clazzName = IVideoHandler.class.getName();
		Cacher.defaultCacher.deleteAllCache(clazzName, "listDeviceHomeItems", 2);
		Cacher.defaultCacher.deleteAllCache(clazzName, "listDeviceHomeItems", 1);
		Cacher.defaultCacher.deleteCache(clazzName, "listDeviceHomeItems", new EHCacheKey(new Object[0]));		
		
		LocationBO.deleteAllCache();
		
		clazzName = IWebsiteLinkHandler.class.getName();
		Cacher.defaultCacher.deleteAllCache(clazzName, "getWebsiteLinkHome", 2);
		Cacher.defaultCacher.deleteCache(clazzName, "listWontubePartnerSites", new EHCacheKey(new Object[0]));		
		
		return true;
	}

	@Override
	public List<WebsiteCategory> listAllWebsiteCategories() {
		return WebsiteCategoryDao.listAllWebsiteCategories();
	}

	@Override
	public boolean saveOrUpdateWebsiteCategory(WebsiteCategory category) {
		return WebsiteCategoryDao.saveOrUpdateWebsiteCategory(category);		
	}

	@Override
	public boolean deleteWebsiteCategory(String pk) {
		return WebsiteCategoryDao.deleteWebsiteCategory(pk);		
	}

	@Override
	public WebsiteCategory getWebsiteCategory(String pk) {
		return WebsiteCategoryDao.getWebsiteCategoryByPK(pk);
	}

	@Override
	public Object[] listWebsiteCategoryAndCountries() {
		List<WebsiteCategory> categories = listAllWebsiteCategories();
		List<Country> countries = CountryDao.listCountries();
		Object[] objs = new Object[2];
		objs[0] = categories;
		objs[1] = countries;
		return objs;
	}

	@Override
	public int addCountry(Country country) {
		return CountryDao.addCountry(country);
	}

	@Override
	public int deleteCountry(String pk) {
		return CountryDao.deleteCountry(pk);
	}
}
